home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 002 / roff4.arc / ROFF42.C < prev    next >
Encoding:
C/C++ Source or Header  |  1985-08-10  |  11.0 KB  |  384 lines

  1. /********************************************************/
  2. /*                                                      */
  3. /*                  ROFF4, Version 1.60                 */
  4. /*                                                      */
  5. /*(C) 1983,4 by Ernest E. Bergmann                      */
  6. /*              Physics, Building #16                   */
  7. /*              Lehigh Univerisity                      */
  8. /*              Bethlehem, Pa. 18015                    */
  9. /*                                                      */
  10. /* Permission is hereby granted for all commercial and  */
  11. /* non-commercial reproduction and distribution of this */
  12. /* material provided this notice is included.           */
  13. /*                                                      */
  14. /********************************************************/
  15. /*feb 19, 1984*/
  16. /*Jan 14, 1984*/
  17. #include "roff4.h"
  18.  
  19. insert()        /*takes a command line in LINE and adds its
  20.                 entry to the table; no space check is made of
  21.                 TRTBL*/
  22. {
  23.       int *pw;
  24.       char *pc,*s2,*s3,*fnd,*find2();
  25.       pw=(int *) TREND;
  26.       *pw=SLINK;
  27.       pc=(char *) (pw+1);
  28.       gettl3(LINE,pc,&s2,&s3);
  29.       if(fnd=find2(pc,SLINK))
  30.       {
  31.             fprintf(STDERR,"%cWarning: <%s> was defined to be <%s>\n",
  32.                         BELL,pc,fnd);
  33.             fprintf(STDERR,"...now it is defined to be <%s>\n",s2);
  34.       }
  35.       SLINK=(int *)TREND;
  36.       TREND=s3;
  37. }
  38. /****************************************/
  39. showit()        /* displays the list of entries in the string
  40.                 substitution table pointed to by HEAD. */
  41. {
  42.       int *pw;
  43.       char *pc;
  44.       fprintf(STDERR,"ENTRIES:\n");
  45.       pw=SLINK;
  46.       while(pw)
  47.       {
  48.             fprintf(STDERR,"%u: ",pw);
  49.             pc=(char *)(pw+1);
  50.             fprintf(STDERR,"<%s>",pc);
  51.             pc += strlen(pc); 
  52.             pc++;
  53.             if(*pc) fprintf(STDERR," <%s>\n",pc);
  54.             else fprintf(STDERR," <>\n"); /*'C' bug*/
  55.             pw=(int *) *pw;
  56.       }
  57.       dashes();
  58. }
  59. /****************************************/
  60. putback(c)      /*cf K & P, p256*/
  61. char c;
  62. {
  63.       if(++BINP >= BACKSIZE)
  64.       {
  65.             printf(STDERR,"Too many characters pushed back\n");
  66.             exit();
  67.       }
  68.       BACKBUF[BINP]=c;
  69. }
  70. /****************************************/
  71. int ngetc(iobuf)        /*cf K & P p256*/
  72. FILE *iobuf;     /*filters \r followed by \n,msb*/
  73. {
  74.       int kgetc();
  75.       int c;
  76.       if(BINP) c=BACKBUF[BINP];
  77.       else    {
  78.             if(KEYBD) c=kgetc();
  79.             else
  80.                 if ((c=getc(iobuf))!=EOF) c &= 0x7f;
  81.             BACKBUF[BINP=1]=c;
  82.       }
  83.       if((c!=EOF)) BINP--;
  84.       if(c=='\r')
  85.       {
  86.             c=ngetc(iobuf);
  87.             if(c!='\n')
  88.             {
  89.                   putback(c);
  90.                   return('\r');
  91.             }
  92.       }
  93.       return(c);
  94. }
  95. /****************************************/
  96. char kgetc()    /*like getc(),from keyboard, line-buffered*/
  97. {
  98.       int i;
  99.       if(!*KPTR)
  100.       {
  101.             fprintf(STDERR,"%c",KEYBD);
  102.             fflush(STDERR);
  103.             if (gets(KLINE)==NULL) return(EOF);
  104.             i=strlen(KLINE);
  105.             KLINE[i++]='\n';
  106.             KLINE[i]='\0';
  107.             KPTR=KLINE;
  108.       }
  109.       return(*(KPTR++));
  110. }
  111. /** add by Conrad Kwok. 9th Sept.,84 ****/
  112. pbmac(s,sl)
  113. char s[],sl[];
  114.       char *parms[10],dupln[LSZ];
  115.       int i;
  116.       strcpy(dupln,sl);
  117.       setparm(dupln,parms);
  118.       for (i=strlen(s)-1;i>=0;)
  119.             if (isdigit(s[i]))
  120.                   if (i>0 && s[i-1]=='$')
  121.                   /*
  122.                           if (parms[s[i] & 0x0f]=='\0')
  123.                           { fprintf(STDERR,
  124.                              "not sufficient parameters in this line\n%s\n",sl);
  125.                             exit();
  126.                           }
  127.                           else
  128.                         */
  129.                   { 
  130.                         pbstr(parms[s[i--] & 0x0f]); 
  131.                         --i;
  132.                   }
  133.                   else
  134.                         putback(s[i--]);     
  135.             else
  136.                   putback(s[i--]);
  137. }
  138. setparm(sl,parms)
  139. char *sl,*parms[];
  140.       char a,c;
  141.       int nuparm;
  142.       for (c=*sl;c!=' ' && c!='\n' && c!='\t';)c=*++sl;
  143.       while (c==' ' || c=='\t')
  144.             c=*++sl;
  145.       for (nuparm=0;nuparm<10;) {
  146.             if (!(isalnum(c) || (c=='+') || (c=='-')))
  147.                   sl++;
  148.             else
  149.                   c=' ';
  150.             parms[nuparm++]=sl;
  151.             for (a=*sl;a!=c && a!='\n' && a!='\0';)
  152.                   if (c==' ' && a=='\t') break;
  153.             else a=*++sl;
  154.             *sl='\0';
  155.             c=a;
  156.             if (a!='\0' && a!='\n') while (c==' ' || c=='\t') c=*++sl;
  157.       }
  158. }
  159. /****************************************/
  160. pbstr(s)        /*put back string on input;cf K&P,p257*/
  161. char s[LSZ];
  162. {
  163.       int i;
  164.       for(i=strlen(s);i>0;) putback(s[--i]);
  165. }
  166. /****************************************/
  167. minsert()       /*takes a .DM and following lines and places
  168.                 the information in the table;  no macro
  169.                 definition nesting permitted*/
  170. {
  171.       char c, *pc,*pc2;
  172.       int *pw1;
  173.       /*pass over command and following white space*/
  174.       for(pc=LINE,c=*pc; (c!=' ')&&(c!='\n')&&(c!='\t'); pc++)
  175.             c=*pc;
  176.       for(; (c==' ')||(c=='\t'); pc++) c=*pc;
  177.       start();
  178.       if(c=='\n') {
  179.             fprintf(STDERR,".DM is UNnamed\n"); 
  180.       }
  181.       else    {
  182.             pw1=(int *)TREND;
  183.             *(pw1++)=MLINK;
  184.             MLINK=(int *)TREND;
  185.             TREND=(char *) pw1;
  186.             while(class(c)==BLACK)
  187.             {
  188.                   *(TREND++)=c;
  189.                   c=*(pc++);
  190.             }
  191.       }
  192.       *(TREND++)='\0';
  193.       while(fgets2(LINE,IOBUF))       /*until EOF*/
  194.       {
  195.             pw1=(int *)LINE;
  196.             if((*LINE==COMMAND)&&(comtyp(LINE)==EM)) break;
  197.             else    {
  198.                   transfer(&pw1,&TREND,0);
  199.                   *(TREND-1)='\n';
  200.             }
  201.       }
  202.       *(TREND++)='\0';
  203.       complete();
  204. }
  205. /****************************************/
  206. showm() /*lists macro definitions*/
  207. {
  208.       int *pw;
  209.       char *pc;
  210.       fprintf(STDERR,"MACROS DEFINED:\n");
  211.       pw=MLINK;
  212.       while(pw)
  213.       {
  214.             pc =(char *) (pw+1);
  215.             fprintf(STDERR,"%u  .%s\n",pw,pc);
  216.             pc +=strlen(pc); 
  217.             pc++;
  218.             fprintf(STDERR,"%s\n",pc);
  219.             pw=(int *)*pw;
  220.       }
  221.       dashes();
  222. }
  223. /****************************************/
  224. char *macq(line)        /*looks up name to see if it is a macro
  225.                         definition.  If it is, returns the
  226.                         corresponding string, else returns
  227.                         FALSE.
  228.                         */
  229. char *line;
  230. {
  231.       char c,*pc,wb[LSZ],*find2();
  232.       pc=wb;
  233.       while(class(c=*(++line))==BLACK) *(pc++)=c;
  234.       *pc='\0';
  235.       return(find2(wb,MLINK));
  236. }
  237. /****************************************/
  238. char *find2(s,link)     /*finds or doesn't find s in table
  239.                         of substitutions pointed to by link*/
  240. char *s;
  241. int *link;
  242. {
  243.       char *pc;
  244.       while(link)
  245.       {
  246.             if(!strcmp(s,link+1))
  247.             {
  248.                   pc=(char *) (link+1);
  249.                   pc += strlen(pc);
  250.                   pc++;
  251.                   return(pc);
  252.             }
  253.             link=(int *) *link;
  254.       }
  255.       return(FALSE);
  256. }
  257. /****************************************/
  258. /*from ndio.c*/
  259. #define CON_INPUT 1                     /* BDOS call to read console       */
  260. #define CON_OUTPUT 2                    /* BDOS call to write to console   */
  261. #define CON_STATUS 11                   /* BDOS call to interrogate status */
  262.  
  263. #define CONTROL_C 3                     /* Quit character                  */
  264. #define INPIPE 2                        /* bit setting to indicate directed
  265. input from a temp. pipe fil     */
  266. #define VERBOSE 2                       /* bit setting to indicate output is to
  267.                                            go to console AND directed output */
  268. #define DIRECTED_OUT 1
  269. #define CONS_TOO 2
  270. #define CPM_LIST_OUT 4
  271. #define PRINTER_OUT 8
  272. #define ROBOTYPE_OUT 16
  273. #define CONSOLE_ONLY 0
  274.  
  275. /* 
  276.         The "dioinit" function must be called at the beginning of the
  277.         "main" function:
  278. */
  279.  
  280. dioflush()
  281. {
  282.       if (_doflag & DIRECTED_OUT)
  283.       {
  284.             putc(0x1a,_dobuf);
  285.             fflush(_dobuf);
  286.             fclose(_dobuf);
  287.       }
  288. }
  289.  
  290. /*
  291.         This version of "putchar" replaces the regular version when using
  292.         directed I/O:
  293. */
  294.  
  295. putchar(c)
  296. char c;
  297. {       
  298.       if (SUPPRESS) return;
  299.       if (LASTCH!='\r' || c!='\r') {
  300.             if (_doflag & DIRECTED_OUT)
  301.             {
  302.                   /* if (c == '\n') putc('\r',_dobuf); */ /*3rd Mar,85   KKP*/
  303.                   if(putc(c,_dobuf) == ERROR)
  304.                   {
  305.                         fprintf(STDERR,"File output error; disk full?\n");
  306.                         exit();
  307.                   }
  308.             }
  309.  
  310.             if (_doflag==0 || _doflag & CONS_TOO)
  311.             {
  312.                   bdos(CON_STATUS);
  313.                   if (c == '\n') bdos(CON_OUTPUT,'\r');
  314.                   bdos(CON_OUTPUT,c);
  315.             }
  316.  
  317.             if (_doflag & CPM_LIST_OUT)
  318.             {
  319.                   /* if (c=='\n') bdos(5,'\r'); */ /*3rd Mar,85 KKP */
  320.                   bdos(5,c);
  321.             }
  322.             LASTCH=c;    /* 5th Mar,85   KKP */
  323.       }
  324. }
  325. /****************************************/
  326. #define argc *argcp
  327. dioinit(argcp,argv)
  328. int *argcp;
  329. char **argv;
  330. {
  331.       int i,j, argcount;
  332.       int n;  /* this keeps track of location in argument */
  333.  
  334.       _doflag = FALSE;  /* No directed /O by default   */
  335.       _nullpos = &argv[argc];
  336.       argcount = 1;
  337.  
  338.       for (i = 1; i < argc; i++)      /* Scan the command line for > and )*/
  339.       {
  340.             n=0;    /* start with first character */
  341. getmore:        
  342.             switch(argv[i][n++]) {
  343.  
  344.             case '+': 
  345.                   _doflag |= VERBOSE;
  346.                   goto getmore;
  347.             case ')':
  348.                   _doflag |= CPM_LIST_OUT;
  349.                   goto getmore;
  350.  
  351. foo:   
  352.             case '&':/*Check for directed output*/
  353.                   if (!argv[i][n]) 
  354.                   {
  355.                         fprintf(STDERR,"Bad redirection specifier\n");
  356.                         exit();
  357.                   }
  358.                   strcpy (sav_out_file, &argv[i][n] );
  359.                   if ((_dobuf=fopen(sav_out_file, "w")) == NULL)
  360.                   {
  361.                         fprintf(STDERR,"\nCan't create <%s>\n",
  362.                         sav_out_file);
  363.                         exit();
  364.                   }
  365.                   _doflag++;
  366.  
  367. movargv:   
  368.                   argc = argcount;
  369.                   argv[argc] = 0;
  370.                   break;
  371.  
  372.             default:    /* handle normal arguments: */
  373.                   if (n!=1) goto movargv;
  374.                   argcount++;
  375.             }
  376.       }
  377. }
  378.  
  379.  
  380. #undef argc
  381.  
  382.